import 'package:fbutton/fbutton.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xm_app/common/logger/LoggerUtil.dart';
import 'package:xm_app/common/ui/AppTheme.dart';
import 'package:xm_app/common/ui/ui_adapter.dart';
import 'package:xm_app/common/ui/ui_image.dart';
import 'package:xm_app/model/FundMNFInfo.dart';
import 'package:xm_app/widget/CommonText.dart';
import 'package:xm_app/widget/SlideTransitionX.dart';
import 'package:xm_app/widget/custom_refresh_widget.dart';

import 'FundController.dart';

class FundPage extends GetView<FundController> {
  @override
  Widget build(BuildContext context) {
    return GetBuilder(
      init: FundController(),
      builder: (_) {
        return Scaffold(
          backgroundColor: Colors.grey.withOpacity(.05),
          appBar: _buildAppBar(),
          body: CustomHeaderRefreshWidget(
            easyRefreshController: controller.easyRefreshController,
            onRefresh: () => controller.getRefresh(),
            slivers: [
              SliverToBoxAdapter(
                child: Column(
                  children: [
                    if (controller.sh000001 != null)
                      Container(
                        padding: EdgeInsets.symmetric(vertical: 5),
                        child: _buildItem(controller.sh000001, showDel: false),
                      ),
                    Container(
                      padding: EdgeInsets.only(left: 5, bottom: 5),
                      child: Row(
                        children: [
                          Icon(
                            Icons.wb_incandescent_outlined,
                            color: AppTheme.pinkColor,
                          ),
                          SizedBox(
                            width: 5,
                          ),
                          CommonText("我的基金")
                        ],
                      ),
                    )
                  ],
                ),
              ),
              if ((controller.fundMNFInfo?.datas?.length ?? 0) > 0)
                SliverList(
                  delegate: SliverChildBuilderDelegate(
                    (BuildContext context, int index) {
                      final item = controller.fundMNFInfo?.datas[index];
                      return _buildItem(item);
                    },
                    childCount: controller.fundMNFInfo?.datas?.length ?? 0,
                  ),
                )
            ],
          ),
        );
      },
    );
  }

  _buildItem(MNFItem item, {bool showDel = true}) {
    return Card(
      child: ValueBuilder<bool>(
        initialValue: false,
        builder: (value, updateFun) {
          return Column(
            children: [
              GestureDetector(
                onTap: () {
                  updateFun(!value);
                },
                child: Container(
                  color: Colors.white,
                  constraints: BoxConstraints(
                    minHeight: getHeight(25),
                  ),
                  child: Row(
                    children: [
                      Container(
                        width: getWidth(70),
                        child: Row(
                          children: [
                            SizedBox(
                              width: 5,
                            ),
                            item.gSZZL.indexOf("-") != -1 ? _downIcon() : _upIcon(),
                            CommonText(
                              "${item.gSZZL}%",
                              txtColor: item.gSZZL.indexOf("-") != -1 ? Colors.green : Colors.red,
                            ),
                          ],
                        ),
                      ),
                      Container(
                        width: getWidth(182),
                        child: Row(
                          children: [
                            CommonText.singleLine(item.sHORTNAME),
                          ],
                        ),
                      ),
                      Container(
                        width: getWidth(75),
                        child: Row(
                          children: [
                            CommonText("(${item.fCODE})"),
                          ],
                        ),
                      ),
                      if (showDel)
                        Expanded(
                          child: GestureDetector(
                            onTap: () => controller.delItemBtn(item),
                            child: Container(
                              padding: EdgeInsets.symmetric(vertical: 5),
                              child: Icon(
                                Icons.delete_outline,
                                color: AppTheme.pinkColor,
                                size: getWidth(18),
                              ),
                            ),
                          ),
                        ),
                    ],
                  ),
                ),
              ),
              if (value)
                Divider(
                  height: 1,
                  color: AppTheme.pinkColor,
                ),
              AnimatedCrossFade(
                firstChild: Container(height: 0.0),
                secondChild: GestureDetector(
                  onDoubleTap: () {
                    updateFun(!value);
                  },
                  child: Container(
                    // padding: EdgeInsets.only(top: 5),
                    height: getHeight(210),
                    child: Center(
                      child: buildNetImage(item.img ??
                          "http://j4.dfcfw.com/charts/pic6/${item.fCODE}.png?v=${DateUtil.formatDate(DateTime.now(), format: DateFormats.y_mo_d_h_m)}"),
                    ),
                  ),
                ),
                firstCurve: const Interval(0.0, 0.6, curve: Curves.fastOutSlowIn),
                secondCurve: const Interval(0.4, 1.0, curve: Curves.fastOutSlowIn),
                sizeCurve: Curves.fastOutSlowIn,
                crossFadeState: value ? CrossFadeState.showSecond : CrossFadeState.showFirst,
                duration: Duration(milliseconds: 400),
              )
            ],
          );
        },
      ),
    );
  }

  _buildAppBar() {
    return AppBar(
        backgroundColor: AppTheme.pinkColor,
        automaticallyImplyLeading: false,
        title: Row(
          children: [
            Expanded(
              child: Container(
                constraints: BoxConstraints(maxHeight: getHeight(30)),
                child: TextField(
                  controller: controller.inputController,
                  autofocus: false,
                  focusNode: controller.focusNode,
                  cursorColor: Colors.black,
                  style: TextStyle(
                    color: Colors.black,
                    fontSize: 14,
                  ),
                  textInputAction: TextInputAction.search,
                  onEditingComplete: () => controller.addFundBtn(),
                  decoration: InputDecoration(
                    contentPadding: EdgeInsets.symmetric(vertical: getHeight(6)),
                    fillColor: Colors.white,
                    filled: true,
                    isDense: true,
                    border: OutlineInputBorder(borderRadius: BorderRadius.all(Radius.circular(50)), borderSide: BorderSide.none),
                    hintText: "海量基金等你来添加",
                    prefixIcon: Icon(
                      Icons.search,
                      color: Colors.grey,
                    ),
                    hintStyle: TextStyle(
                      color: Colors.grey,
                      fontSize: 14,
                      textBaseline: TextBaseline.alphabetic,
                    ),
                    suffixIcon: controller.inputController.text.length > 0
                        ? GestureDetector(
                            onTap: () => controller.cleanText(),
                            child: Icon(
                              Icons.cancel,
                              color: Color(0xFFcccccc),
                            ),
                          )
                        : Text(""),
                  ),
                ),
              ),
            ),
            // Spacer(),
            ValueBuilder<Color>(
              initialValue: Colors.white,
              builder: (value, updateFn) {
                return GestureDetector(
                  onTapDown: (_) {
                    logDebug("onTapDown");
                    updateFn(Colors.blue);
                  },
                  onTap: () {
                    logDebug("onTap");
                    controller.addFundBtn();
                    Future.delayed(Duration(milliseconds: 100), () => updateFn(Colors.white));
                  },
                  child: Padding(
                    padding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
                    child: CommonText(
                      "添加",
                      txtColor: value,
                      txtSize: getSp(14),
                    ),
                  ),
                );
              },
            ),
          ],
        ));
  }

  _upIcon() {
    return Icon(
      Icons.arrow_upward,
      color: Colors.red,
      size: getSp(15),
    );
  }

  _downIcon() {
    return Icon(
      Icons.arrow_downward,
      color: Colors.green,
      size: getSp(15),
    );
  }
}
